package in.muton.edushare.poll.sections;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.questions.QuestionBase;
import in.muton.edushare.poll.util.QuestionFactory;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollPages;
import com.edushare.common.vo.PollQuestions;

public class PollSection extends EdusharePanel {
	private static final long serialVersionUID = -3887825386005536187L;
	public static final Log log = LogFactory.getLog(PollSection.class);
	private boolean preview;

	public boolean isPreview() {
		return preview;
	}

	private IModel<Poll> pollModel;
	private IModel<PollPages> pageModel;
	private List<QuestionBase> qbList = new ArrayList<QuestionBase>();

	public PollSection(String id, IModel<Poll> model,
			final EduShareBasePage page, int pageNum, boolean preview) {
		super(id, model, page);
		this.preview = preview;
		this.pollModel = model;
		this.pageModel = getPageByNum(pollModel, pageNum);

		add(new Label("pageTitle", new Model<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject() {
				return "Page " + pageModel.getObject().getPageNumber() + " : "
						+ pageModel.getObject().getPageTitle();
			}
		}));

		add(new ListView<QuestionBase>("questionList",
				new IModel<List<QuestionBase>>() {
					private static final long serialVersionUID = -6503889674463740333L;

					public List<QuestionBase> getObject() {
						return getQuestionPanels();
					}

					public void setObject(List<QuestionBase> arg0) {
					}

					public void detach() {
					}
				}) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<QuestionBase> arg0) {
				arg0.add(arg0.getModelObject());
			}
		});
	}

	private List<QuestionBase> getQuestionPanels() {
		log.debug("Current question list size = " + qbList.size());
		log.debug("Current page questions = "
				+ pageModel.getObject().getQuestions().size());
		if (qbList.size() == pageModel.getObject().getQuestions().size())
			return qbList;
		List<PollQuestions> pqList = pageModel.getObject().getQuestions();
		Collections.sort(pqList);
		for (PollQuestions pq : pqList) {
			qbList.add(QuestionFactory.getQuestionPanel("question",
					new CayenneModel<PollQuestions>(pq), getBasePage(),
					preview, false));
		}
		log.debug("Updated questions list size = " + qbList.size());
		return qbList;
	}

	public boolean save(boolean draft) {
		boolean retVal = true;
		for (QuestionBase qb : qbList) {
			if (retVal)
				retVal = qb.save(draft);
		}
		return retVal;
	}

	public boolean verify() {
		boolean retVal = true;
		for (QuestionBase qb : qbList) {
			if (retVal)
				retVal = qb.verify();
		}
		return retVal;
	}

	public void nextPage() {
		qbList.clear();
		this.pageModel = getPageByNum(pollModel, pageModel.getObject()
				.getPageNumber() + 1);
	}

	public void prevPage() {
		qbList.clear();
		this.pageModel = getPageByNum(pollModel, pageModel.getObject()
				.getPageNumber() - 1);
	}

	public void clear() {
		for (QuestionBase qb : qbList) {
			qb.clear();
		}
	}

	public int getCurrPageNum() {
		return pageModel.getObject().getPageNumber();
	}

	public IModel<PollPages> getCurrentPage() {
		return pageModel;
	}

	public static IModel<PollPages> getPageByNum(IModel<Poll> pollModel, int num) {
		List<PollPages> pollPages = pollModel.getObject().getPages();
		for (PollPages p : pollPages) {
			if (p.getPageNumber() == num)
				return new CayenneModel<PollPages>(p);
		}
		return new CayenneModel<PollPages>(pollModel.getObject().getPages()
				.get(0));
	}

}
